package com.jtyoui.intecepter;

import com.jtyoui.controller.ChatController;
import com.jtyoui.model.User;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.messaging.support.ChannelInterceptorAdapter;


public class SocketChannelInterceptor extends ChannelInterceptorAdapter {

    @Override
    public void postSend(Message<?> message, MessageChannel channel, boolean sent) {
        StompHeaderAccessor headerAccessor = StompHeaderAccessor.wrap(message);
        if (headerAccessor.getCommand() == null) return;
        String sessionId = headerAccessor.getSessionAttributes().get("sessionId").toString();
        switch (headerAccessor.getCommand()) {
            case CONNECT:
                break;
            case DISCONNECT:
                disconnect(sessionId);
                break;
            case SUBSCRIBE:
                break;
            case UNSUBSCRIBE:
                break;
            default:
                break;
        }
    }

    private void disconnect(String sessionId) {
        System.out.println("移除" + sessionId);
        ChatController.onlineUser.remove(sessionId);
    }
}
